﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using log4net;
using IDML.BusinessObject;

namespace IDML.DataAccess
{
   public class TestDAL
    {
         private DBConnection _dbConnect;

         public TestDAL()
        {
            _dbConnect = new DBConnection();
        }

         public List<TestStructureBO> GetTestStructure_Listst(TestBO testBO)
        {
            SqlConnection connection = null;
            DataSet ds = null;
            TestStructureBO testStructure = null;
            List<TestStructureBO> reportTextList = null;
            try
            {
                connection = _dbConnect.OpenConnection();
                _dbConnect.BeginTransaction();
                SqlCommand selectCommand = new SqlCommand("GetTestStructure_List", connection);
                selectCommand.CommandType = CommandType.StoredProcedure;
                selectCommand.Parameters.Add("@TestId", SqlDbType.Int).Value = testBO.TestId;
                SqlDataAdapter da = new SqlDataAdapter(selectCommand);
                ds = new DataSet();
                _dbConnect.CommitTransaction();
                da.Fill(ds);
                _dbConnect.CloseConnection();

                reportTextList = new List<TestStructureBO>();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    testStructure = new TestStructureBO();
                    testStructure.TestStructureId = int.Parse(row["TestStructureId"].ToString());
                    testStructure.TestId = int.Parse(row["TestId"].ToString());
                    testStructure.TestStructureText = row["TestStructureText"].ToString();
                    testStructure.TextOrder = int.Parse(row["TextOrder"].ToString());
                    testStructure.ControlId = int.Parse(row["ControlId"].ToString());
                    testStructure.DependTestStructureId =row["DependTestStructureId"].ToString()==""?0: int.Parse(row["DependTestStructureId"].ToString());
                    testStructure.Operator = row["Operator"].ToString();
                    reportTextList.Add(testStructure);
                }
                return reportTextList;
            }
            catch (Exception ex)
            {
                string error = ex.Message;
                //Log.Error("GetUser", ex);
                _dbConnect.RollbackTransaction();
                _dbConnect.CloseConnection();
            }
            finally
            {
                _dbConnect.CloseConnection();
            }
            return reportTextList;
        }

         public List<MasterListDataBO> GetMasterListData(MasterListDataBO masterListDataBO)
         {
             SqlConnection connection = null;
             DataSet ds = null;
             MasterListDataBO masterListData = null;
             List<MasterListDataBO> masterListDatatList = null;
             try
             {
                 connection = _dbConnect.OpenConnection();
                 _dbConnect.BeginTransaction();
                 SqlCommand selectCommand = new SqlCommand("GetMasterListData", connection);
                 selectCommand.CommandType = CommandType.StoredProcedure;
                 selectCommand.Parameters.Add("@TestStructureId", SqlDbType.Int).Value = masterListDataBO.TestStructureId;
                 SqlDataAdapter da = new SqlDataAdapter(selectCommand);
                 ds = new DataSet();
                 _dbConnect.CommitTransaction();
                 da.Fill(ds);
                 _dbConnect.CloseConnection();

                 masterListDatatList = new List<MasterListDataBO>();
                 foreach (DataRow row in ds.Tables[0].Rows)
                 {
                     masterListData = new MasterListDataBO();
                     masterListData.MasterListDataId = int.Parse(row["MasterListDataId"].ToString());
                     masterListData.TestStructureId = int.Parse(row["TestStructureId"].ToString());
                     masterListData.Value = row["Value"].ToString();

                     masterListDatatList.Add(masterListData);
                 }
                 return masterListDatatList;
             }
             catch (Exception ex)
             {
                 string error = ex.Message;
                 //Log.Error("GetUser", ex);
                 _dbConnect.RollbackTransaction();
                 _dbConnect.CloseConnection();
             }
             finally
             {
                 _dbConnect.CloseConnection();
             }
             return masterListDatatList;
         }

         public List<TestFeeStructDetailsBO> GetTestFeeStructure(TestBO testBO)
         {
             SqlConnection connection = null;
             DataSet ds = null;
             TestFeeStructDetailsBO testfeestructureDet = null;
             List<TestFeeStructDetailsBO> testfeestructureDetList = null;

             try
             {
                 connection = _dbConnect.OpenConnection();
                 _dbConnect.BeginTransaction();
                 SqlCommand selectCommand = new SqlCommand("GetTestFeeStructure", connection);
                 selectCommand.CommandType = CommandType.StoredProcedure;
                 selectCommand.Parameters.Add("@TestCode", SqlDbType.NVarChar).Value = testBO.TestCode;
                 SqlDataAdapter da = new SqlDataAdapter(selectCommand);
                 ds = new DataSet();
                 _dbConnect.CommitTransaction();
                 da.Fill(ds);
                 _dbConnect.CloseConnection();

                 testfeestructureDet = new TestFeeStructDetailsBO();
                 testfeestructureDetList = new List<TestFeeStructDetailsBO>();

                 testfeestructureDet.TestId = int.Parse(ds.Tables[0].Rows[0]["TestId"].ToString());
                 testfeestructureDet.TestFeeStructureId = int.Parse(ds.Tables[0].Rows[0]["TestFeeStructureId"].ToString());
                 testfeestructureDet.TestCode = ds.Tables[0].Rows[0]["TestCode"].ToString();
                 testfeestructureDet.TestName = ds.Tables[0].Rows[0]["TestName"].ToString();
                 testfeestructureDet.Amount = Convert.ToDecimal(ds.Tables[0].Rows[0]["Amount"].ToString());

                 testfeestructureDetList.Add(testfeestructureDet);

             }
             catch (Exception ex)
             {
                 string error = ex.Message;
                 _dbConnect.RollbackTransaction();
                 _dbConnect.CloseConnection();
             }
             finally
             {
                 _dbConnect.CloseConnection();
             }

             return testfeestructureDetList;
         }

         public List<ValidationBO> GetValidationData(ValidationBO validationBo)
         {
             SqlConnection connection = null;
             DataSet ds = null;
             ValidationBO validation = null;
             List<ValidationBO> validationDatatList = null;
             try
             {
                 connection = _dbConnect.OpenConnection();
                 _dbConnect.BeginTransaction();
                 SqlCommand selectCommand = new SqlCommand("GetValidation", connection);
                 selectCommand.CommandType = CommandType.StoredProcedure;
                 selectCommand.Parameters.Add("@TestId", SqlDbType.Int).Value = validationBo.TestId;
                 SqlDataAdapter da = new SqlDataAdapter(selectCommand);
                 ds = new DataSet();
                 _dbConnect.CommitTransaction();
                 da.Fill(ds);
                 _dbConnect.CloseConnection();

                 validationDatatList = new List<ValidationBO>();
                 foreach (DataRow row in ds.Tables[0].Rows)
                 {
                     validation = new ValidationBO();
                     validation.TestId = int.Parse(row["TestId"].ToString());
                     validation.TestStructureId = int.Parse(row["TestStructureId"].ToString());
                     validation.ValidationId = int.Parse(row["ValidationId"].ToString());
                     validation.ValidationTypeId = int.Parse(row["ValidationTypeId"].ToString());
                     validation.Message = row["Message"].ToString();
                     validation.Expression = row["Expression"].ToString();
                     validationDatatList.Add(validation);
                 }
                 return validationDatatList;
             }
             catch (Exception ex)
             {
                 string error = ex.Message;
                 //Log.Error("GetUser", ex);
                 _dbConnect.RollbackTransaction();
                 _dbConnect.CloseConnection();
             }
             finally
             {
                 _dbConnect.CloseConnection();
             }
             return validationDatatList;
         }

         public int PatientTestResult_Insert(PatientTestResultBO patientTestResultBo)
         {
             SqlConnection connection = null;

             try
             {
                 connection = _dbConnect.OpenConnection();
                 _dbConnect.BeginTransaction();
                 SqlCommand insertcommand = new SqlCommand("PatientTestResult_Insert", connection);
                 insertcommand.CommandType = CommandType.StoredProcedure;
                 insertcommand.Parameters.Add("@PatientId", SqlDbType.Int).Value = patientTestResultBo.PatientId;
                 insertcommand.Parameters.Add("@TestStructureId", SqlDbType.Int).Value = patientTestResultBo.TestStructureId;
                 insertcommand.Parameters.Add("@Value", SqlDbType.NVarChar).Value = patientTestResultBo.Value; 
          
                 _dbConnect.CommitTransaction();
                 int patid = Convert.ToInt32(insertcommand.ExecuteScalar());
                 _dbConnect.CloseConnection();
                 return patid;
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             finally
             {
                 _dbConnect.CloseConnection();
             }

         }




    }
}
